import {
	defineStore
} from 'pinia';
import {
	getInfo
} from '@/utils/app.js';

export const useUserStore = defineStore('user', {
	state: () => ({
		token: uni.getStorageSync('token') || '',
		userInfo: uni.getStorageSync('userInfo') || {},
		projectList: [],
		supplierList: [],
		statusList: [],
		materialList: [],
        isLoading: false,
        isLogin:false, // 
	}),
	actions: {
		// 设置 Token
		setToken(token) {
			this.token = token;
            uni.setStorageSync('token', token);
            this.isLogin = !!token;
		},
        setIsLogin(status) {
            this.isLogin = status
        },
		// 获取用户信息
		getInfoDeatil() {
			return new Promise((resolve, reject) => {
				getInfo()
					.then((res) => {
						if (res&&res.code == 200) {
							this.userInfo = res.user;
							uni.setStorageSync('userInfo', res.user);
							resolve(res);
						} else {
							reject(res);
						}
					})
					.catch((error) => {
						reject(error);
					});
			});
		},

		// 退出登录
		logout() {
			this.token = '';
			this.userInfo = {};
			this.projectList = [];
			this.supplierList = [];
			this.statusList = [];
			uni.removeStorageSync('token');
			uni.removeStorageSync('userInfo');
		}
    },
    getters: {
        isLoginStatus: (state) => state.isLogin
    },
	persist: {
		storage: {
			// 修改存储方式
			getItem: uni.getStorageSync,
			setItem: uni.setStorageSync
		}
	}
});